[Python] How to use re match objects in a list comprehension

Posted by Brent.Longborough on Stack Overflow See other posts from Stack Overflow or by Brent.Longborough
Published on 2010-03-12T23:51:02Z Indexed on 2010/03/12 23:57 UTC
Read the original article Hit count: 424

Filed under:
|
|

I have a function to pick out lumps from a list of strings and return them as another list:

def filterPick(lines,regex):
    result = []
    for l in lines:
        match = re.search(regex,l)
        if match:
            result += [match.group(1)]
    return result

Is there a way to reformulate this as a list comprehension? Obviously it's fairly clear as is; just curious.

© Stack Overflow or respective owner

Related posts about python

Related posts about regex